  /*
	@Author: 于辉
	@Time: 2020-11
	@Tittle: rolePowers
	@Description: 角色授权管理
*/
var vue , form ;
 layui.use(["form", 'jquery',"table"],function(){
 	form = layui.form;
 	
 	$("#roleList").height($(window).height()-100);
 	$(window).resize(function(){
		$("#roleList").height($(window).height()-100);
	})
 	vue = new Vue({
 		el:"#powers",
 		data:{
 			selectRoleId:'',
 			search:'',
 			rolelist:[],
 			powersShowList:[],
 			powersList:[]
 		},
 		watch:{
 			search:function (newValue, oldValue){
 				var powersShowList = [];
 				vue.powersList.forEach(v => {
 					if(newValue == '' || v.page.indexOf(newValue)>-1){
						powersShowList.push(v);
					}
				});
				vue.powersShowList = powersShowList;
 			}
 		},
 		updated:function(){
		    form.render();
		},
		methods:{
			changeRole:function(roleid){
				vue.selectRoleId = roleid;
				getRolePowers(roleid);
			},
			changeCheckboxChecked:function(bl){
				vue.powersList.forEach(v => {
					v.powers.forEach(sub => {
						sub.checked = bl
					});
				});
			},
			changeItemCheck:function(roleid,powerid){
				vue.powersList[roleid].powers[powerid].checked = !vue.powersList[roleid].powers[powerid].checked;
			},
			savePowers:function(){
				var powerids = [];
				vue.powersList.forEach(v => {
		            v.powers.forEach(sub => {
			            if (sub.checked ) {
			              powerids.push(sub.id)
			            }
		            })
		        })
				ajaxSetting({
					url:getServerPath("UPDATEROLEPOWERS"),
					data:JSON.stringify({ "roleId": vue.selectRoleId,  "powerIds": powerids}),
					success:function(){
						layer.msg("保存成功",{icon:6})
					}
				})
			}
		}
 	})
 	//获取所有角色列表
 	getRoleList();
 	//获取角色权限列表
 	getPowersGroup();
 	
 });
 
 /*
  * 获取所有角色列表
  * */
 var getRoleList = function(){
 	ajaxSetting({
 		url:getServerPath("GETROLEMODEL"),
 		type:"get",
 		data:{id:''},
 		success:function(res){
 			vue.rolelist = res.data;
 			if(vue.rolelist.length>0){
 				vue.selectRoleId = vue.rolelist[0].id;
 				getRolePowers(vue.rolelist[0].id)
 			}
 			
 		}
 	})
 }
 /*
  * 获取角色权限列表
  * */
var getPowersGroup = function(){
	ajaxSetting({
 		url:getServerPath("GETPOWERSGROUP"),
 		type:"get",
 		success:function(res){
 			vue.powersList = res.data;
 		}
 	})
}
/*
 * 查询角色授权 
 * */
var getRolePowers = function(roleid){
	ajaxSetting({
 		url:getServerPath("GETROLEPOWERS"),
 		type:"get",
 		data:{roleId:roleid},
 		success:function(res){	
 			vue.powersList.forEach(v => {
	            v.powers.forEach(sub => {
		            if (res.data.indexOf(sub.id) === -1) {
		              sub.checked = false
		            } else {
		              sub.checked = true
		            }
	            })
	        })
 			vue.powersShowList = vue.powersList;
 			
 			
 		}
 	})
}

